import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login'
import home from '../components/home'
import wekcome from '../components/wekcome'
import users from '../components/user/users'
import rights from '../components/power/rights'
import roles from '../components/power/roles'
import cate from '../components/goods/cate'
import params from '../components/goods/params'
import list from '../components/goods/list'
import add from '../components/goods/add'
import order from '../components/orders/order'
import report from '../components/report/report.vue'


Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login',//重定向
  },
  {
    path:'/login',
    name:'login',
    component:login
  },{
    path:'/home',
    name:'home',
    component:home,
    redirect:'/wekcome',
    children:[
      {
        path:'/wekcome',
        name:'wekcome',
        component:wekcome
      },{
        path:'/users',//用户列表
        name:'users',
        component:users
      },{
        path:'/rights',//权限列表
        name:'rights',
        component:rights
      },{
        path:'/roles',//角色列表
        name:'roles',
        component:roles
      },{
        path:'/categories',//商品分类
        name:'cate',
        component:cate
      },{
        path:'/params',//分类参数
        name:'params',
        component:params
      },{
        path:'/goods',//shagnp列表
        name:'list',
        component:list
      },{
        path:'/goods/add',//添加商品
        name:'add',
        component:add
      },{
        path:'/orders',//订单管理
        name:'order',
        component:order
      },{
        path:'/reports',//数据统计
        name:'report',
        component:report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.name === 'login') return next()
  // 获取token
  const tokenstr=sessionStorage.getItem('token');
  if(!tokenstr) return next('/login')
  next()
})
export default router
